What is for-own?
The for-own npm package is a utility for iterating over the own enumerable properties of an object. It provides a simple and efficient way to loop through an object's properties, executing a callback function for each property.
What are for-own's main functionalities?
Iterate over object properties
This feature allows you to iterate over the own enumerable properties of an object. The callback function receives the value and key of each property.
const forOwn = require('for-own');
const obj = { a: 1, b: 2, c: 3 };
forOwn(obj, function(value, key) {
console.log(key + ': ' + value);
});
Early exit from iteration
This feature allows you to exit the iteration early by returning false from the callback function. In this example, the iteration stops when the key 'b' is encountered.
const forOwn = require('for-own');
const obj = { a: 1, b: 2, c: 3 };
forOwn(obj, function(value, key) {
if (key === 'b') return false;
console.log(key + ': ' + value);
});
Other packages similar to for-own
lodash
Lodash is a modern JavaScript utility library delivering modularity, performance, and extras. It includes a `forOwn` method that provides similar functionality to the for-own package, but with additional features and optimizations.
underscore
Underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It includes a `_.each` method that can be used to iterate over object properties, similar to for-own.
object-keys
The object-keys package is a simple utility that provides a method to get the own enumerable property names of an object. While it doesn't provide iteration directly, it can be used in conjunction with a loop to achieve similar functionality to for-own.
for-own
Iterate over the own enumerable properties of an object, and return an object with properties that evaluate to true from the callback. Exit early by returning false
. JavaScript/Node.js.
Install
Install with npm:
$ npm install --save for-own
Usage
var forOwn = require('for-own');
var obj = {a: 'foo', b: 'bar', c: 'baz'};
var values = [];
var keys = [];
forOwn(obj, function (value, key, o) {
keys.push(key);
values.push(value);
});
console.log(keys);
console.log(values);
About
Related projects
- arr-flatten: Recursively flatten an array or arrays. This is the fastest implementation of array flatten. | homepage
- collection-map: Returns an array of mapped values from an array or object. | homepage
- for-in: Iterate over the own and inherited enumerable properties of an object, and return an object… more | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Contributors
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb
Running tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
Author
Jon Schlinkert
License
Copyright © 2017, Jon Schlinkert.
Released under the MIT License.
This file was generated by verb-generate-readme, v0.4.2, on February 26, 2017.